package com.only4play.importapt.web.resources;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.only4play.common.model.GeneralResponse;
import com.only4play.importapt.model.entity.UserEntity;
import com.only4play.importapt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserResource
 * @brief UserResource
 * @details UserResource
 * @date 2024-05-21
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-05-21               liyuncong          Created
 */

@RestController
@RequestMapping(path = "/api/v1/users")
public class UserResource {

    @Autowired
    private UserService userService;

    @GetMapping("/pageQuery")
    public GeneralResponse<?> pageQuery(
        @RequestParam("pageNo") Integer pageNo,
        @RequestParam("pageSize") Integer pageSize
    ) {
        IPage<UserEntity> page = new Page<>(pageNo, pageSize);
        IPage<UserEntity> data = userService.page(page);
        return GeneralResponse.of(data);
    }

    @GetMapping("/queryByUserName")
    public GeneralResponse<?> queryByUserName(
        @RequestParam("userName") String userName
    ) {
        LambdaQueryWrapper<UserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(UserEntity::getUserName, userName);
        List<UserEntity> list = userService.list(lambdaQueryWrapper);
        return GeneralResponse.of(list);
    }

}
